package com.example.controller;

import com.example.pojo.Headline;
import com.example.service.HeadlineService;
import com.example.utils.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Yun
 * @Date: 2024/3/15
 */
@RestController
@RequestMapping("/headline")
@CrossOrigin
public class HeadLineController {
    @Autowired
    private HeadlineService headlineService;

    @PostMapping("/publish")
    public Result publish(@RequestBody Headline headline, @RequestHeader String token) {
        Result result = headlineService.publish(headline, token);
        return result;
    }

    @PostMapping("/findHeadlineByHid")
    public Result findHeadlineByHid(@Param("hid") Integer hid) {
        Headline headline = headlineService.getById(hid);
        Map data = new HashMap();
        data.put("headline", headline);
        return Result.ok(data);
    }

    @PostMapping("/update")
    public Result update(@RequestBody Headline headline) {
        Result result = headlineService.updateData(headline);
        return result;
    }

    @PostMapping("/removeByHid")
    public Result removeByHid(@Param("hid") Integer hid) {
        headlineService.removeById(hid);
        return Result.ok(null);
    }
}
